<template>
  <div>
    <!--    分割线-->
    <el-divider></el-divider>
    <!--    |基本资料-->
    <span style="font-weight:bold"><span style="color:royalblue">|</span>&nbsp;基本资料</span>
    <el-divider></el-divider>
    <!--    添加表单-->
    <el-form :label-position="labelPosition" :rules="rules" label-width="80px" :model="systemUserForm">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="用户名:" prop="userName">
            <el-input v-model="systemUserForm.userName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="密码:" prop="userPwd">
            <el-input v-model="systemUserForm.userPwd"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="姓名:" prop="hrEmpId">
            <el-select style="width: 100%" clearable v-model="systemUserForm.hrEmpId" placeholder="请选择员工">
              <el-option
                  v-for="su in hrEmpData"
                  :key="su.id"
                  :label="su.empName"
                  :value="su.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用户组:" prop="systemRoleId">
            <el-select style="width: 100%" clearable v-model="systemUserForm.systemRoleId" placeholder="请选择角色">
              <el-option
                  v-for="su in sysRoleData"
                  :key="su.id"
                  :label="su.roleName"
                  :value="su.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="用户状态:">
            <el-radio-group v-model="systemUserForm.userStatus">
              <el-radio label="Y">启用</el-radio>
              <el-radio label="N">禁用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="addBut">
      <el-button type="primary" plain @click="addSysUser">提交</el-button>
      <el-button type="primary" plain @click="goBack">返回</el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: "SystemUserInsert",
  data() {
    return {
      //表单对齐方式：顶部
      labelPosition: 'top',
      //表单字段
      systemUserForm: {
        userName: "",
        userPwd: "",
        hrEmpId: "",
        systemRoleId: "",
        userStatus: "Y",
        biaoshi: 2
      },
      //小红星验证规则
      rules: {
        userName: [
          {required: true, message: '请输入用户名', trigger: 'blur'}
        ],
        userPwd: [
          {required: true, message: '请输入用户密码', trigger: 'blur'}
        ],
        hrEmpId: [
          {required: true, message: '请选择员工', trigger: 'change'}
        ],
        systemRoleId: [
          {required: true, message: '请选择角色', trigger: 'change'}
        ]
      },
      // 员工下拉框数据
      hrEmpData: [],
      // 角色下拉框数据
      sysRoleData: []
    }
  },
  created() {
    this.getHrEmp(),
        this.getSystemRole()
  },
  methods: {
    //查询员工下拉框数据
    getHrEmp() {
      this.$http.get('hrEmp', {params: {"chaxun": "1"}})
          .then((res) => {
            this.hrEmpData = res.data.data.list;
          })
          .catch(function (error) {
            console.log(error);
          });
    },
    //查询角色下拉框数据
    getSystemRole() {
      // 为给定 ID 的 user 创建请求
      this.$http.get('systemRole')
          .then((res) => {
            this.sysRoleData = res.data.data.list;
          })
          .catch(function (error) {
            console.log(error);
          });
    },
    //添加
    addSysUser() {
      this.$http.post('systemUser', this.systemUserForm)
          .then(
              this.$notify({
                title: '成功',
                message: '添加成功!',
                type: 'success'
              }),
                  this.$router.push({name: "SystemUser"})
          )
          .catch(function (error) {
            console.log(error);
          });
    },
    //页头返回
    goBack() {
      this.$router.push("SystemUser")
    }
  }
}
</script>

<style scoped>
.addBut {
  text-align: center;
}
</style>